package com.gxar.timeline.server.base.controller;

import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.timeline.server.base.model.ResultCodeEnum;
import com.gxar.timeline.server.base.model.hmd.vo.request.HmdPageRequest;
import com.gxar.timeline.server.base.model.hmd.vo.response.HmdImportResponse;
import com.gxar.timeline.server.base.model.hmd.vo.response.HmdPageResponse;
import com.gxar.timeline.server.db.service.HmdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/9/21 17:22
 */
@Api(tags = "头显控制器")
@RestController
@RequestMapping("/api/timeline/hmd")
@RequiredArgsConstructor
@Slf4j
public class HmdController {

    private final HmdService hmdService;

    @ApiOperation("头显排行列表")
    @PostMapping("/list")
    public Result<PageResult<HmdPageResponse>> pageList(@RequestBody HmdPageRequest request) {
        return hmdService.pageList(request);
    }

    @ApiOperation("导入头显")
    @PostMapping("/import")
    public Result<HmdImportResponse> importHmd(@RequestParam(name = "file", required = false) MultipartFile file) {
        HmdImportResponse hmdImportResponse;
        try {
            hmdImportResponse = hmdService.importHmd(file);
        } catch (IOException e) {
            log.error("excel[{}]导入失败: {}", file.getName(), e.getMessage());
            throw new APIRuntimeException(ResultCodeEnum.EXCEL_ERROR);
        }
        return Result.success(hmdImportResponse);
    }
}
